﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using stdole;
using Swsk33.FinalCourseDesign.DataObject;

namespace Swsk33.FinalCourseDesign.src.Util
{
	/// <summary>
	/// 在地图上生成标记的实用类
	/// </summary>
	public class DrawMarker
	{
		private static IElement singleInstructMarker;

		private static IElement singleInstructText;

		/// <summary>
		/// 作一唯一指示标记
		/// </summary>
		/// <param name="mapControl">传入地图控件</param>
		/// <param name="x">点击x坐标</param>
		/// <param name="y">点击y坐标</param>
		public static void DrawSingleInstructMarker(AxMapControl mapControl, int x, int y)
		{
			// 获取地图容器
			IGraphicsContainer container = mapControl.ActiveView.GraphicsContainer;
			// 先移除上次标志
			if (singleInstructMarker != null)
			{
				container.DeleteElement(singleInstructMarker);
			}
			if (singleInstructText != null)
			{
				container.DeleteElement(singleInstructText);
			}
			IPoint clickPoint = mapControl.ToMapPoint(x, y);
			// 创建点标志
			IRgbColor color = new RgbColor();
			color.Red = 255;
			color.Green = 0;
			color.Blue = 185;
			ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
			markerSymbol.Size = 13;
			markerSymbol.Color = color;
			markerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
			markerSymbol.YOffset = 6.5;
			singleInstructMarker = new MarkerElementClass();
			((IMarkerElement)singleInstructMarker).Symbol = markerSymbol;
			singleInstructMarker.Geometry = clickPoint;
			// 创建文字标志
			singleInstructText = new TextElementClass();
			ITextSymbol textSymbol = new TextSymbolClass();
			StdFont font = new StdFont();
			font.Name = "微软雅黑";
			font.Size = 11;
			font.Bold = true;
			textSymbol.Font = (IFontDisp)font;
			((ITextElement)singleInstructText).Symbol = textSymbol;
			string xStr = clickPoint.X.ToString();
			xStr = xStr.Substring(0, xStr.IndexOf(".") + 4);
			string yStr = clickPoint.Y.ToString();
			yStr = yStr.Substring(0, yStr.IndexOf(".") + 4);
			((ITextElement)singleInstructText).Text = "已设定为当前位置，经纬度：" + xStr + ", " + yStr;
			singleInstructText.Geometry = mapControl.ToMapPoint(x, y - 25);
			// 加入地图
			container.AddElement(singleInstructText, 0);
			container.AddElement(singleInstructMarker, 0);
			mapControl.Refresh();
		}

		private static IElement singleCanteenMarker;

		private static IElement singleCanteenText;

		/// <summary>
		/// 作一唯一食堂标记
		/// </summary>
		/// <param name="mapControl">传入地图控件</param>
		/// <param name="canteen">餐厅对象</param>
		/// <param name="store">门店对象</param>
		public static void DrawSingleCanteenMarker(AxMapControl mapControl, Canteen canteen, Store store)
		{
			// 获取地图容器
			IGraphicsContainer container = mapControl.ActiveView.GraphicsContainer;
			// 先移除上次标志
			if (singleCanteenMarker != null)
			{
				container.DeleteElement(singleCanteenMarker);
			}
			if (singleCanteenText != null)
			{
				container.DeleteElement(singleCanteenText);
			}
			// 创建点标志
			IRgbColor color = new RgbColor();
			color.Red = 225;
			color.Green = 119;
			color.Blue = 0;
			ISimpleMarkerSymbol markerSymbol = new SimpleMarkerSymbolClass();
			markerSymbol.Size = 13;
			markerSymbol.Color = color;
			markerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;
			markerSymbol.YOffset = 6.5;
			singleCanteenMarker = new MarkerElementClass();
			((IMarkerElement)singleCanteenMarker).Symbol = markerSymbol;
			IPoint canteenLocation = new PointClass();
			canteenLocation.X = canteen.Longitude;
			canteenLocation.Y = canteen.Latitude;
			singleCanteenMarker.Geometry = canteenLocation;
			// 创建文字标志
			singleCanteenText = new TextElementClass();
			ITextSymbol textSymbol = new TextSymbolClass();
			StdFont font = new StdFont();
			font.Name = "微软雅黑";
			font.Size = 11;
			font.Bold = true;
			textSymbol.Font = (IFontDisp)font;
			((ITextElement)singleCanteenText).Symbol = textSymbol;
			((ITextElement)singleCanteenText).Text = "推荐去：" + canteen.Name + " " + store.Name;
			IPoint canteenTextPosition = new PointClass();
			canteenTextPosition.X = canteen.Longitude;
			canteenTextPosition.Y = canteen.Latitude + 0.00045;
			singleCanteenText.Geometry = canteenTextPosition;
			// 加入地图
			container.AddElement(singleCanteenText, 0);
			container.AddElement(singleCanteenMarker, 0);
			mapControl.Refresh();
		}
	}
}